<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qqmlextensionplugin.cpp -->
  <title>QQmlExtensionPlugin  类 | Qt QML 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtqml-index.html">Qt QML</a></td><td ><a href="qtqml-module.html">C++ 类</a></td><td >QQmlExtensionPlugin</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtqml-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level1"><a href="#public-functions">公用函数</a></li>
<li class="level1"><a href="#reimplemented-public-functions"><span lang="zh-cn">
重新实现的</span>公用函数</a></li>
<li class="level1"><a href="#static-public-members">静态公用成员</a></li>
<li class="level1"><a href="#details">详细信息</a></li>
<li class="level2"><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlextensionplugin.html#timeexample-qml-extension-plugin">TimeExample QML 扩展插件</a></li>
	<li><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlextensionplugin.html#project-settings-for-the-plugin">插件的项目设置</a></li>
<li class="level2"><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlextensionplugin.html#plugin-definition-in-the-qmldir">qmldir 中的插件定义</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QQmlExtensionPlugin 类</h1>
<!-- $$$QQmlExtensionPlugin-brief -->
<p><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlextensionplugin.html">QQmlExtensionPlugin</a>类提供了自定义的QML扩展插件的抽象基。<a href="#details">更多...</a></p>
<!-- @@@QQmlExtensionPlugin -->
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> 头文件：</td><td class="memItemRight bottomAlign">   <span class="preprocessor">#include &lt;QQmlExtensionPlugin&gt;</span>
</td></tr><tr><td class="memItemLeft rightAlign topAlign"> qmake:</td><td class="memItemRight bottomAlign"> QT += qml</td></tr><tr><td class="memItemLeft rightAlign topAlign"> 
	<span lang="zh-cn">引入于：</span></td><td class="memItemRight bottomAlign">  Qt 5.0</td></tr><tr><td class="memItemLeft rightAlign topAlign">继承自：</td><td class="memItemRight bottomAlign"> <a href="../qtcore/qobject.html">QObject</a></td></tr></table></div><ul>
<li><a href="qqmlextensionplugin-members.html"></a></li>
<li><a href="qqmlextensionplugin-obsolete.html">废弃的成员</a></li>
</ul>
<a name="public-functions"></a>
<h2 id="public-functions">公用函数</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qqmlextensionplugin.html#QQmlExtensionPlugin">QQmlExtensionPlugin</a></b>(QObject *<i>parent</i> = nullptr)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QUrl </td><td class="memItemRight bottomAlign"><b><a href="qqmlextensionplugin.html#baseUrl">baseUrl</a></b>() const</td></tr>
</table></div>
<a name="reimplemented-public-functions"></a>
<h2 id="reimplemented-public-functions"><span lang="zh-cn">重新实现的</span>公用函数</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> virtual void </td><td class="memItemRight bottomAlign"><b><a href="qqmlextensionplugin.html#initializeEngine">initializeEngine</a></b>(QQmlEngine *<i>engine</i>, const char *<i>uri</i>) override</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual void </td><td class="memItemRight bottomAlign"><b><a href="qqmlextensionplugin.html#registerTypes">registerTypes</a></b>(const char *<i>uri</i>) override = 0</td></tr>
</table></div>
<ul>
<li class="fn">31 个公用函数继承自<a href="../qtcore/qobject.html#public-functions">QObject</a></li>
</ul>
<a name="static-public-members"></a>
<h2 id="static-public-members">静态公用成员</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> const QMetaObject </td><td class="memItemRight bottomAlign"><b><a href="qqmlextensionplugin.html#staticMetaObject-var">staticMetaObject</a></b></td></tr>
</table></div>
<ul>
<li class="fn">9 个静态公用成员继承自<a href="../qtcore/qobject.html#static-public-members">QObject</a></li>
</ul>
<h3>额外的继承成员</h3>
<ul>
<li class="fn">1 个属性继承自<a href="../qtcore/qobject.html#properties">QObject</a></li>
<li class="fn">1 个公用槽函数继承自<a href="../qtcore/qobject.html#public-slots">QObject</a></li>
<li class="fn">2 个信号继承自<a href="../qtcore/qobject.html#signals">QObject</a></li>
<li class="fn">9 个保护函数继承自<a href="../qtcore/qobject.html#protected-functions">QObject</a></li>
</ul>
<a name="details"></a>
<!-- $$$QQmlExtensionPlugin-description -->
<div class="descr">
<h2 id="details">详细信息</h2>
<p>
<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlextensionplugin.html">
QQmlExtensionPlugin</a>类提供了自定义的QML扩展插件的抽象基。</p>
<p><a href="qqmlextensionplugin.html">QQmlExtensionPlugin</a>是一个插件接口，它可以创建可以动态加载到QML应用程序中的QML扩展。这些扩展允许QML引擎可以使用自定义QML类型。</p>
<p>编写 QML 扩展插件：</p>
<ol class="1" type="1"><li><span lang="zh-cn">子类化</span> <a href="qqmlextensionplugin.html">QQmlExtensionPlugin</a><ul>
<li>使用<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtcore/qtplugin.html#Q_PLUGIN_METADATA">Q_PLUGIN_METADATA</a> ()宏向Qt元对象系统注册插件</li>
<li>覆盖<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlextensionplugin.html#registerTypes">registerTypes()</a>方法并调用<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlengine.html#qmlRegisterType">qmlRegisterType</a> () 注册插件要导出的类型</li>
</ul>
</li>
<li>为插件编写项目文件</li>
<li>创建一个<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qtqml-modules-qmldir.html">qmldir 文件</a>来描述插件</li>
</ol>
<p>QML 扩展插件适用于特定于应用程序或类库的插件。库插件应该限制自己注册类型，因为对引擎根上下文的任何操作都可能导致库用户代码中的冲突或其他问题。</p>
<a name="timeexample-qml-extension-plugin"></a>
<h3 >TimeExample QML 扩展插件</h3>
<p>假设有一个新的<code>TimeModel</code>C++ 类应该作为新的 QML 类型提供。它提供当前时间<code>hour</code>和<code>minute</code>属性。</p>
<pre class="cpp">

  <span class="keyword">class</span> TimeModel : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qobject.html#QObject">QObject</a></span>
  {
      Q_OBJECT
      Q_PROPERTY(<span class="type">int</span> hour READ hour NOTIFY timeChanged)
      Q_PROPERTY(<span class="type">int</span> minute READ minute NOTIFY timeChanged)
      ...

</pre>
<p>为了使这个类型可用，我们创建了一个名为<a href="qqmlextensionplugin.html">QQmlExtensionPlugin</a>的插件类，它是<a href="qqmlextensionplugin.html">QQmlExtensionPlugin</a>的子类。它覆盖<a href="qqmlextensionplugin.html#registerTypes">registerTypes()</a>()方法，以便使用<a href="qqmlengine.html#qmlRegisterType">qmlRegisterType</a>()注册TimeModel类型。它还在类定义中使用<a href="../qtcore/qtplugin.html#Q_PLUGIN_METADATA">Q_PLUGIN_METADATA</a>()宏，使用插件的唯一标识符将插件注册到Qt元对象系统中。</p>
<pre class="cpp">

  <span class="keyword">class</span> <span class="type">QExampleQmlPlugin</span> : <span class="keyword">public</span> <span class="type"><a href="qqmlextensionplugin.html#QQmlExtensionPlugin">QQmlExtensionPlugin</a></span>
  {
      Q_OBJECT
      Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid)

  <span class="keyword">public</span>:
      <span class="type">void</span> registerTypes(<span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>uri) override
      {
          Q_ASSERT(uri <span class="operator">=</span><span class="operator">=</span> QLatin1String(<span class="string">&quot;TimeExample&quot;</span>));
          qmlRegisterType<span class="operator">&lt;</span>TimeModel<span class="operator">&gt;</span>(uri<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="string">&quot;Time&quot;</span>);
      }
  };

</pre>
<p>这将TimeModel类注册到这个插件库的<code>1.0</code>版本中，作为称为<code>Time</code>的QML类型。宏<a href="../qtcore/qtglobal.html#Q_ASSERT">Q_ASSERT</a>()可以确保使用此插件的任何QML组件都正确导入类型名称空间。<a href="qtqml-cppintegration-definetypes.html">C++的定义QML类型</a>文章提供了更多关于将C++类型注册到运行时的信息。</p>
<a name="project-settings-for-the-plugin"></a>
<h3 >插件的项目设置</h3>
<p>此外，项目文件(.pro)将项目定义为一个插件库，指定它应该内建到<code>imports/TimeExample</code>目录中，并注册插件目标名称和其他各种细节:</p>
<pre class="cpp">

  TEMPLATE <span class="operator">=</span> lib
  CONFIG <span class="operator">+</span><span class="operator">=</span> qt plugin
  QT <span class="operator">+</span><span class="operator">=</span> qml

  DESTDIR <span class="operator">=</span> imports<span class="operator">/</span>TimeExample
  TARGET <span class="operator">=</span> qmlqtimeexampleplugin
  SOURCES <span class="operator">+</span><span class="operator">=</span> qexampleqmlplugin<span class="operator">.</span>cpp

</pre>
<a name="plugin-definition-in-the-qmldir"></a>
<h3 >qmldir 中的插件定义</h3>
<p>最后，在<code>imports/TimeExample</code>目录中需要一个<a href="qtqml-modules-qmldir.html">qmldir文件</a>来描述插件及其导出的类型。该插件包括一个时钟。QML文件以及由项目构建的<code>qmlqtimeexampleplugin</code> 
(如上所示在.pro文件中)，所以这两个都需要在qmldir文件中指定:</p>
<pre class="cpp">

  module TimeExample
  Clock <span class="number">1.0</span> Clock<span class="operator">.</span>qml
  plugin qmlqtimeexampleplugin

</pre>
<p>为了让这个例子更简单，我们让TimeExample的源目录在<code>imports/TimeExample</code>中，并且直接从<a href="../qtdoc/configure-options.html#source-build-and-install-directories">源代码构建</a>。源目录的结构并不是那么重要，因为qmldir文件可以指定安装QML文件的路径。</p>
<p>重要的是 qmldir 安装到的目录的名称。当用户导入我们的模块时，QML 引擎使用<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qtqml-modules-qmldir.html#contents-of-a-module-definition-qmldir-file">模块标识符</a>( <code>TimeExample</code>) 来查找插件，因此安装它的目录必须与模块标识符匹配。</p>
<p>构建并安装项目后<code>Time</code> ，任何导入<code>TimeExample</code> 模块的QML 组件都可以访问新组件</p>
<pre class="qml">

  import TimeExample 1.0 <span class="comment">// import types from the plugin</span>

  <span class="type">Clock</span> { <span class="comment">// this class is defined in QML (imports/TimeExample/Clock.qml)</span>

      <span class="type">Time</span> { <span class="comment">// this class is defined in C++ (plugin.cpp)</span>
          <span class="name">id</span>: <span class="name">time</span>
      }

      <span class="name">hours</span>: <span class="name">time</span>.<span class="name">hour</span>
      <span class="name">minutes</span>: <span class="name">time</span>.<span class="name">minute</span>

  }

</pre>
<p><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qtqml-qmlextensionplugins-example.html">插件示例中</a>提供了完整的源代码。</p>
<p>在<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qtqml-tutorials-extending-qml-example.html">写作QML扩展与C ++</a>教程还包含有关创建QML插件的一章。</p>
</div>
<p><b>请参考 </b><a href="qqmlengine.html#importPlugin">QQmlEngine::importPlugin</a>()
<span lang="zh-cn">和</span> <a href="../qtdoc/plugins-howto.html">
<span lang="zh-cn">如何创建Qt插件</span></a><span lang="zh-cn">。</span></p>
<!-- @@@QQmlExtensionPlugin -->
<div class="func">
<h2>成员函数文档</h2>
<!-- $$$QQmlExtensionPlugin[overload1]$$$QQmlExtensionPluginQObject* -->
<h3 class="fn" id="QQmlExtensionPlugin"><a name="QQmlExtensionPlugin"></a>QQmlExtensionPlugin::<span class="name">QQmlExtensionPlugin</span>(<span class="type"><a href="../qtcore/qobject.html#QObject">QObject</a></span> *<i>parent</i> = nullptr)</h3>
<p>使用给定的<i>parent</i>构造 QML 扩展插件。</p>
<p>请注意，此构造函数由<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtcore/qtplugin.html#Q_PLUGIN_METADATA">Q_PLUGIN_METADATA</a> () 宏自动调用，因此无需显式调用它。</p>
<!-- @@@QQmlExtensionPlugin -->
<!-- $$$baseUrl[overload1]$$$baseUrl -->
<h3 class="fn" id="baseUrl"><a name="baseUrl"></a><span class="type"><a href="../qtcore/qurl.html">QUrl</a></span> QQmlExtensionPlugin::<span class="name">baseUrl</span>() const</h3>
<p>返回加载扩展的目录的 URL。</p>
<p>当插件还需要从同一目录加载 QML 文件或其他<span lang="zh-cn">资源</span>时，这很有用。</p>
<p>这个函数在Qt 5.1中引入。</p>
<!-- @@@baseUrl -->
<!-- $$$initializeEngine[overload1]$$$initializeEngineQQmlEngine*constchar* -->
<h3 class="fn" id="initializeEngine"><a name="initializeEngine"></a><code>[override virtual] </code><span class="type">void</span> QQmlExtensionPlugin::<span class="name">initializeEngine</span>(<span class="type"><a href="qqmlengine.html">QQmlEngine</a></span> *<i>engine</i>, const <span class="type">char</span> *<i>uri</i>)</h3>
<p>使用<i>engine</i>从<i>uri</i>初始化扩展。例如，应用程序插件可能会向 QML 公开一些数据或对象，作为引擎根上下文的上下文属性。</p>
<!-- @@@initializeEngine -->
<!-- $$$registerTypes[overload1]$$$registerTypesconstchar* -->
<h3 class="fn" id="registerTypes"><a name="registerTypes"></a><code>[override pure virtual] </code><span class="type">void</span> QQmlExtensionPlugin::<span class="name">registerTypes</span>(const <span class="type">char</span> *<i>uri</i>)</h3>
<p>在给定的<i>uri 中</i>注册 QML 类型。子类应该实现这一点，以便为扩展插件提供的所有类型调用<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlengine.html#qmlRegisterType">qmlRegisterType</a> ()。</p>
<p>该<i><span lang="zh-cn">uri</span></i>是用于通过基于所述名称和扩展的插件库的路径的QML发动机产生的插件的标识符。</p>
<!-- @@@registerTypes -->
</div>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
